import { Profile } from 'mixly';

Profile = Profile ?? {};

const pins = {};

if (typeof Profile.generate !== 'function') {
    Profile.parse = function (range) {
        let pinList = [];
        for (let i of range) {
            const pinInfo = i.split('-');
            let pinNumStr0, pinNumStr1;
            switch (pinInfo.length) {
            case 1:
                pinNumStr0 = pinInfo[0].toString();
                if (!isNaN(pinNumStr0)) {
                    const pinNum = parseInt(pinNumStr0);
                    pinList.push(pinNum);
                }
                break;
            case 2:
                pinNumStr0 = pinInfo[0].toString();
                pinNumStr1 = pinInfo[1].toString();
                if (!isNaN(pinNumStr0) && !isNaN(pinNumStr1)) {
                    let pinNum0 = parseInt(pinNumStr0);
                    let pinNum1 = parseInt(pinNumStr1);
                    if (pinNum0 < 0 || pinNum1 < 0) break;
                    if (pinNum0 > pinNum1) {
                        [ pinNum0, pinNum1 ] = [ pinNum1, pinNum0 ];
                    }
                    for (let j = pinNum0; j <= pinNum1; j++) {
                        if (!pinList.includes(j)) {
                            pinList.push(j);
                        }
                    }
                }
                break;
            }
        }
        return pinList;
    };

    Profile.generate = function (pinMap, add1 = '', add2 = '') {
        const getPins = (list) => {
            let pins = [];
            for (let i of list) {
                const pin = [ add1 + i, add2 + i ];
                pins.push(pin);
            }
            return pins;
        }
        const pinList = this.parse(pinMap);
        return getPins(pinList);
    };
}

pins['arduino_stm32f103c'] = {
    description: 'STM32F103C',
    digital: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['13-15'], 'PC', 'PC')
    ],
    analog: [
        ...Profile.generate(['0-7'], 'PA', 'PA'),
        ...Profile.generate(['0'], 'PB', 'PB')
    ],
    pwm: [
        ...Profile.generate(['0-3', '6-10'], 'PA', 'PA'),
        ...Profile.generate(['0', '6-7'], 'PB', 'PB')
    ],
    interrupt: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['13-15'], 'PC', 'PC')
    ],
    SDA: [
        ['PB7','PB7']
    ],
    SCL: [
        ['PB6','PB6']
    ],
    MOSI: [
        ['PA7','PA7']
    ],
    MISO: [
        ['PA6','PA6']
    ],
    SCK: [
        ['PA5','PA5']
    ],
    serial_select: [
        ['Serial', 'Serial'],
        ['Serial1', 'Serial1'],
        ['Serial2', 'Serial2'],
        ['Serial3', 'Serial3'],
        ['SoftwareSerial', 'mySerial'],
        ['SoftwareSerial1', 'mySerial1'],
        ['SoftwareSerial2', 'mySerial2'],
        ['SoftwareSerial3', 'mySerial3']
    ],
    serial : 9600
};

pins['arduino_stm32f103r'] = {
    description: 'STM32F103R',
    digital: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['0-15'], 'PC', 'PC'),
        ...Profile.generate(['0-2'], 'PD', 'PD')
    ],
    analog: [
        ...Profile.generate(['0-7'], 'PA', 'PA'),
        ...Profile.generate(['0-1'], 'PB', 'PB'),
        ...Profile.generate(['0-5'], 'PC', 'PC')
    ],
    pwm: [
        ...Profile.generate(['0-3', '6-10'], 'PA', 'PA'),
        ...Profile.generate(['0-1', '6-9'], 'PB', 'PB'),
        ...Profile.generate(['6-9'], 'PC', 'PC')
    ],
    interrupt: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['0-15'], 'PC', 'PC'),
        ...Profile.generate(['0-2'], 'PD', 'PD')
    ],
    SDA: [
        ['PB7','PB7']
    ],
    SCL: [
        ['PB6','PB6']
    ],
    MOSI: [
        ['PA7','PA7']
    ],
    MISO: [
        ['PA6','PA6']
    ],
    SCK: [
        ['PA5','PA5']
    ],
    serial_select: [
        ['Serial', 'Serial'],
        ['Serial1', 'Serial1'],
        ['Serial2', 'Serial2'],
        ['Serial3', 'Serial3'],
        ['SoftwareSerial', 'mySerial'],
        ['SoftwareSerial1', 'mySerial1'],
        ['SoftwareSerial2', 'mySerial2'],
        ['SoftwareSerial3', 'mySerial3']
    ],
    serial : 9600
};

pins['arduino_stm32f103t'] = {
    description: 'STM32F103T',
    digital: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-7'], 'PB', 'PB')
    ],
    analog: [
        ...Profile.generate(['0-7'], 'PA', 'PA'),
        ...Profile.generate(['0'], 'PB', 'PB')
    ],
    pwm: [
        ...Profile.generate(['0-3', '6-10'], 'PA', 'PA'),
        ...Profile.generate(['0', '6-7'], 'PB', 'PB')
    ],
    interrupt: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-7'], 'PB', 'PB')
    ],
    SDA: [
        ['PB7','PB7']
    ],
    SCL: [
        ['PB6','PB6']
    ],
    MOSI: [
        ['PA7','PA7']
    ],
    MISO: [
        ['PA6','PA6']
    ],
    SCK: [
        ['PA5','PA5']
    ],
    serial_select: [
        ['Serial', 'Serial'],
        ['Serial1', 'Serial1'],
        ['Serial2', 'Serial2'],
        ['Serial3', 'Serial3'],
        ['SoftwareSerial', 'mySerial'],
        ['SoftwareSerial1', 'mySerial1'],
        ['SoftwareSerial2', 'mySerial2'],
        ['SoftwareSerial3', 'mySerial3']
    ],
    serial : 9600
};

pins['arduino_stm32f103v'] = {
    description: 'STM32F103V',
    digital: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['0-15'], 'PC', 'PC'),
        ...Profile.generate(['0-15'], 'PD', 'PD'),
        ...Profile.generate(['0-15'], 'PE', 'PE')
    ],
    analog: [
        ...Profile.generate(['0-7'], 'PA', 'PA'),
        ...Profile.generate(['0-1'], 'PB', 'PB'),
        ...Profile.generate(['0-5'], 'PC', 'PC')
    ],
    pwm: [
        ...Profile.generate(['0-3', '6-10'], 'PA', 'PA'),
        ...Profile.generate(['0-1', '6-9'], 'PB', 'PB'),
        ...Profile.generate(['6-9'], 'PC', 'PC')
    ],
    interrupt: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['0-15'], 'PC', 'PC'),
        ...Profile.generate(['0-15'], 'PD', 'PD'),
        ...Profile.generate(['0-15'], 'PE', 'PE')
    ],
    SDA: [
        ['PB7','PB7']
    ],
    SCL: [
        ['PB6','PB6']
    ],
    MOSI: [
        ['PA7','PA7']
    ],
    MISO: [
        ['PA6','PA6']
    ],
    SCK: [
        ['PA5','PA5']
    ],
    serial_select: [
        ['Serial', 'Serial'],
        ['Serial1', 'Serial1'],
        ['Serial2', 'Serial2'],
        ['Serial3', 'Serial3'],
        ['SoftwareSerial', 'mySerial'],
        ['SoftwareSerial1', 'mySerial1'],
        ['SoftwareSerial2', 'mySerial2'],
        ['SoftwareSerial3', 'mySerial3']
    ],
    serial : 9600
};

pins['arduino_stm32f103z'] = {
    description: 'STM32F103Z',
    digital: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['0-15'], 'PC', 'PC'),
        ...Profile.generate(['0-15'], 'PD', 'PD'),
        ...Profile.generate(['0-15'], 'PE', 'PE'),
        ...Profile.generate(['0-15'], 'PF', 'PF'),
        ...Profile.generate(['0-15'], 'PG', 'PG')
    ],
    analog: [
        ...Profile.generate(['0-7'], 'PA', 'PA'),
        ...Profile.generate(['0-1'], 'PB', 'PB'),
        ...Profile.generate(['0-5'], 'PC', 'PC')
    ],
    pwm: [
        ...Profile.generate(['0-3', '6-10'], 'PA', 'PA'),
        ...Profile.generate(['0-1', '6-9'], 'PB', 'PB'),
        ...Profile.generate(['6-9'], 'PC', 'PC')
    ],
    interrupt: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['0-15'], 'PC', 'PC'),
        ...Profile.generate(['0-15'], 'PD', 'PD'),
        ...Profile.generate(['0-15'], 'PE', 'PE'),
        ...Profile.generate(['0-15'], 'PF', 'PF'),
        ...Profile.generate(['0-15'], 'PG', 'PG')
    ],
    SDA: [
        ['PB7','PB7']
    ],
    SCL: [
        ['PB6','PB6']
    ],
    MOSI: [
        ['PA7','PA7']
    ],
    MISO: [
        ['PA6','PA6']
    ],
    SCK: [
        ['PA5','PA5']
    ],
    serial_select: [
        ['Serial', 'Serial'],
        ['Serial1', 'Serial1'],
        ['Serial2', 'Serial2'],
        ['Serial3', 'Serial3'],
        ['SoftwareSerial', 'mySerial'],
        ['SoftwareSerial1', 'mySerial1'],
        ['SoftwareSerial2', 'mySerial2'],
        ['SoftwareSerial3', 'mySerial3']
    ],
    serial : 9600
};

pins['arduino_stm32f401'] = {
    description: 'STM32F401',
    digital: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['13-15'], 'PC', 'PC')
    ],
    analog: [
        ...Profile.generate(['0-7'], 'PA', 'PA'),
        ...Profile.generate(['0'], 'PB', 'PB')
    ],
    pwm: [
        ...Profile.generate(['0-3', '6-10'], 'PA', 'PA'),
        ...Profile.generate(['0', '6-7'], 'PB', 'PB')
    ],
    interrupt: [
        ...Profile.generate(['0-15'], 'PA', 'PA'),
        ...Profile.generate(['0-15'], 'PB', 'PB'),
        ...Profile.generate(['13-15'], 'PC', 'PC')
    ],
    SDA: [
        ['PB7','PB7']
    ],
    SCL: [
        ['PB6','PB6']
    ],
    MOSI: [
        ['PA7','PA7']
    ],
    MISO: [
        ['PA6','PA6']
    ],
    SCK: [
        ['PA5','PA5']
    ],
    serial_select: [
        ['Serial', 'Serial'],
        ['Serial1', 'Serial1'],
        ['Serial2', 'Serial2'],
        ['Serial3', 'Serial3'],
        ['SoftwareSerial', 'mySerial'],
        ['SoftwareSerial1', 'mySerial1'],
        ['SoftwareSerial2', 'mySerial2'],
        ['SoftwareSerial3', 'mySerial3']
    ],
    serial : 9600
};

pins['Generic STM32F103C series']  =
pins['Generic STM32F103C6/fake STM32F103C8'] = pins['arduino_stm32f103c'];
pins['Generic STM32F103R series'] = pins['arduino_stm32f103r'];
pins['Generic STM32F103T series'] = pins['arduino_stm32f103t'];
pins['Generic STM32F103V series'] = pins['arduino_stm32f103v'];
pins['Generic STM32F103Z series'] = pins['arduino_stm32f103z'];
pins['Blackpill STM32F401CCU6'] = pins['arduino_stm32f401'];

export default pins;